﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace Auction_Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form.Count > 0)
            {
                if (Request.Form["type"] == "loop")
                {
                    ChatTask task = new ChatTask(this.Context);
                    task._chatNow = this.ChatNow;//有新的消息的回调方法，即为通知处理，这个时候释放掉连接
                    string user = Request.Form["user"].Trim();
                    PageAsyncTask async = new PageAsyncTask(new BeginEventHandler(task.OnBegin), new EndEventHandler(task.OnEnd), new EndEventHandler(task.OnTimeout), user);
                    Page.RegisterAsyncTask(async);
                    Page.ExecuteRegisteredAsyncTasks();//异步执行

                }
            }
        }
        private void ChatNow(string source, string msg)
        {
            Response.Write("{" +
                string.Format(
                "source:\"{0}\",msg:\"{1}\",time:\"{2}\"", source, msg, DateTime.Now.ToString("hh:mm:ss"))
            + "}");
        }
    }
}